package br.com.automed.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

@FacesConverter(value="com.automed.marcaraFoneConverter")
public class MascaraTelefoneConverter implements Converter {

	public Object getAsObject(FacesContext context, UIComponent component, String value) {
		
		if(value != null){
			value = value.replaceAll("[-()]", "");
		}
	
		return value;
	}

	public String getAsString(FacesContext context, UIComponent componen, Object value) {
		if(value != null && !value.toString().isEmpty()){
			String ddd = value.toString().substring(0, 2);
			String primeiraParte = value.toString().substring(2, 6);
			String segundaParte = value.toString().substring(6, 10);
			value = "("+ddd+")"+primeiraParte+"-"+segundaParte;
		}
		return value != null ? value.toString() : null;
	}

}
